package linux;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SCPInputStream;
import ch.ethz.ssh2.Session;

import java.io.*;

/**
 * @description: TODO 远程Linux 使用jar ganymed-ssh2-262.jar
 * @author: wzj
 * @create: 2018/11/1 16:29
 */
public class LinuxUtils {
    private Connection conn;

    /**
     * 连接linux服务器
     * @param ip
     * @param port
     * @param user
     * @param pwd
     * @return 成功返回true
     */
    public boolean openConn(String ip, int port, String user, String pwd){
        conn=new Connection(ip,port);
        try {
            conn.connect();
            return conn.authenticateWithPassword(user,pwd);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 关闭连接
     */
    public void closeConn(){
        conn.close();
    }

    /**
     * 执行命令 没有输出结果
     * @param cmd
     */
    public void exeCmd(String cmd){
        try {
            Session session= conn.openSession();
            session.execCommand(cmd);
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行有输出的命令
     * @param cmd
     * @return
     */
    public String exeCmdOut(String cmd){
        String result=null;
        try {
            Session session= conn.openSession();
            session.execCommand(cmd);
            result=processStdout(session.getStdout());
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 读取InputStream流，返回字符串
     * @param is
     * @return
     */
    public String processStdout(InputStream is){
        StringBuffer sb = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            while((line=br.readLine()) != null){
                System.out.println(line);
                sb.append(line+"\r\n");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 上传文件到服务器
     * @param remoteFileName
     * @param localFile
     * @param remoteDirectory
     */
    public void upFile(String remoteFileName,File localFile,String remoteDirectory){
        SCPClient scpClient=new SCPClient(conn);
        try {
            //默认的文件模式为0600，当前用户具有rw权限
            scpClient.put(remoteFileName,localFile.length(),remoteDirectory,null).close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载文件到本地
     * @param remoteFilePath
     * @param localDirectory
     * @param localFileName
     */
    public void downFile(String remoteFilePath,String localDirectory,String localFileName){
        SCPClient scpClient=new SCPClient(conn);
        try {
            SCPInputStream scpIs=scpClient.get(remoteFilePath);
            File file = new File(localDirectory);
            if(!file.exists()){
                file.mkdirs();
            }
            file = new File(localDirectory+"/"+localFileName);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] b = new byte[4096];
            int i;
            while ((i = scpIs.read(b)) != -1){
                fos.write(b,0, i);
            }
            fos.flush();
            fos.close();
            scpIs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
